import java.util.Scanner;

/*Use synchronized to solve the problem, But it not make sure that "C1" will running firstly
 * So it will print a double foofoo when the "C2" run first
*/
public class Bate1 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("please input n ");
		int n = sc.nextInt();
		FooBar FB = new FooBar(n);
		C1 c1 = new C1(FB);
		C2 c2 = new C2(FB);
		c1.start();
		c2.start();
	}

}

class C1 extends Thread{
	FooBar FB;
	public C1(FooBar FB) {
		this.FB = FB;
	}
	public void run() {
		FB.bar();
	}
}
class C2 extends Thread{
	FooBar FB;
	public C2(FooBar FB) {
		this.FB = FB;
	}
	public void run() {
		FB.foo();
	}
}
class FooBar {
	private int n;
	public FooBar(int n) {
		this.n = n;
		}
	public synchronized  void foo() {
		for (int i = 0; i < n; i++) {
			this.notify();
			System.out.print("foo");
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public synchronized void bar() {
		for (int i = 0; i < n; i++) {
			this.notify();
			try {
				this.wait(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("bar");
		}
	}
}